/*
 *  Copyright 2019-2020  Sun Feng
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.ruibo.modules.weixin.service.impl;

import com.ruibo.modules.weixin.domain.ReportAttachment;
import com.ruibo.utils.*;
import lombok.RequiredArgsConstructor;
import com.ruibo.modules.weixin.repository.ReportAttachmentRepository;
import com.ruibo.modules.weixin.service.ReportAttachmentService;
import com.ruibo.modules.weixin.service.dto.ReportAttachmentDto;
import com.ruibo.modules.weixin.service.dto.ReportAttachmentQueryCriteria;
import com.ruibo.modules.weixin.service.mapstruct.ReportAttachmentMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * @author 孙峰
 * @website https://el-admin.vip
 * @description 服务实现
 * @date 2020-11-06
 **/
@Service
@RequiredArgsConstructor
public class ReportAttachmentServiceImpl implements ReportAttachmentService {

    private final ReportAttachmentRepository reportAttachmentRepository;
    private final ReportAttachmentMapper reportAttachmentMapper;

    @Override
    public Map<String, Object> queryAll(ReportAttachmentQueryCriteria criteria, Pageable pageable) {
        Page<ReportAttachment> page = reportAttachmentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(reportAttachmentMapper::toDto));
    }

    @Override
    public List<ReportAttachmentDto> queryAll(ReportAttachmentQueryCriteria criteria) {
        return reportAttachmentMapper.toDto(reportAttachmentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    @Transactional
    public ReportAttachmentDto findById(Long id) {
        ReportAttachment reportAttachment = reportAttachmentRepository.findById(id).orElseGet(ReportAttachment::new);
        ValidationUtil.isNull(reportAttachment.getId(), "ReportAttachment", "id", id);
        return reportAttachmentMapper.toDto(reportAttachment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReportAttachmentDto create(ReportAttachment resources) {
        return reportAttachmentMapper.toDto(reportAttachmentRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ReportAttachment resources) {
        ReportAttachment reportAttachment = reportAttachmentRepository.findById(resources.getId()).orElseGet(ReportAttachment::new);
        ValidationUtil.isNull(reportAttachment.getId(), "ReportAttachment", "id", resources.getId());
        UpdateUtil.copyNullProperties(resources, reportAttachment);
        reportAttachmentRepository.save(reportAttachment);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            reportAttachmentRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ReportAttachmentDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ReportAttachmentDto reportAttachment : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put(" createdBy", reportAttachment.getCreatedBy());
            map.put(" createdDate", reportAttachment.getCreatedDate());
            map.put(" lastModifiedBy", reportAttachment.getLastModifiedBy());
            map.put(" lastModifiedDate", reportAttachment.getLastModifiedDate());
            map.put(" remark", reportAttachment.getRemark());
            map.put(" version", reportAttachment.getVersion());
            map.put(" attachmentType", reportAttachment.getAttachmentType());
            map.put(" dustReportId", reportAttachment.getDustReportId());
            map.put(" fileName", reportAttachment.getFileName());
            map.put(" url", reportAttachment.getUrl());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    /**
     *  根据案件id查询附加你信息
     * @param dustReportId
     * @return
     */
    @Override
    public List<ReportAttachmentDto> findByDustReportId(Long dustReportId) {
        List<ReportAttachment> feedback = reportAttachmentRepository.findByDustReportIdAndRemark(dustReportId, "feedback");
        return reportAttachmentMapper.toDto(feedback);
    }
}
